// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.0
import "CalendarJS.js" as CalFunctions
import "Language.js" as Language

/*
  Dont mind the color scheme, just coding / testing it out for now.
 */
Rectangle{
    id:main;
    //var daysOfWeek = ["Sunday","Monday","Tuesday","Wednesday","Thurday","Friday"];
    property variant months: ["January","February","March","April","May","June","July","August","September","October","November","December"];
    property variant daysInMonth: [31,28,31,30,31,30,31,31,30,31,30,31];
    width: 579;
    height: 600;
    color: "black";
    property string lang
    Item {
        id: titleBox2

        width: 400
        height: 40
        z: 1

        LeftTitle{
            x: 10
            y: -25
            text: {if(lang=="1") qsTr("calendar");else qsTr("calendario");}
            size: 32
        }
    }
    MouseArea {
        id: mouseRegion
        anchors.fill: parent;
        property variant clickedPosition: "1,1"
        x: 0
        y: 0
        width: 576
        height: 500
        anchors.topMargin: 0

        onPressed: {
            clickedPosition  = Qt.point(mouse.x,mouse.y)
        }
        onPositionChanged: {
            var mousePosX = (mouse.x <0) ? 0 : mouse.x
           var mousePosY = (mouse.y <0) ? 0 : mouse.y
            var relative = Qt.point(mousePosX-clickedPosition.x, mousePosY-clickedPosition.y)
            main.pos = Qt.point(main.pos.x+relative.x,
                                    main.pos.y+relative.y)

        }
    }

        Grid {
            columns: 1;
            rows: 5;
            spacing: 2;

            Rectangle{
                id:calendarTitle;
                color: "forestgreen"
                width: 579;
                height: 50
                border.color: "black"
                border.width: 1;

                Image {
                    width: 30;
                    height: 30;
                    y: 10;
                    smooth: true
                    source: "./Assets/Calendar/prev.png"

                    MouseArea{
                        z:10
                        anchors.fill:parent;
                        acceptedButtons: Qt.LeftButton;
                        onClicked:{
                            CalFunctions.calcMonthPrev();
                            monthTitle.text = months[CalFunctions.monthIndex] + ", " + CalFunctions.calcYear();
                            repeatDays.model = (daysInMonth[CalFunctions.monthIndex]+CalFunctions.first_DOW);
                            console.log("Clicked");
                        }
                    }
                }
                Text{
                    id:monthTitle;
                    text: months[CalFunctions.monthIndex] + ", " + CalFunctions.calcYear();
                    font.pointSize: 30
                    anchors.centerIn: parent
                }

                Image {
                    width: 30;
                    height: 30;
                    x:545
                    y: 10;
                    smooth: true
                    source: "./Assets/Calendar/next.png"
               // Text{
                 //   x:545
                 //   y:30
                 //   text: "Next"
                //    font.pointSize: 10
                  //  anchors.right: home.left
                  //  anchors.leftMargin: 10
                    MouseArea{
                        z:10
                        anchors.fill:parent;
                        acceptedButtons: Qt.LeftButton;
                        onClicked:{
                            CalFunctions.calcMonthNext();
                            monthTitle.text = months[CalFunctions.monthIndex] + ", " + CalFunctions.calcYear();
                            repeatDays.model = (daysInMonth[CalFunctions.monthIndex]+CalFunctions.first_DOW);
                            console.log("Clicked");
                        }
                    }
                }
            }
        }

        Rectangle{
            id:calDays;
            y: 50;
            width: 579;
            height: 20;
            color: "#bfbfbf";
            Grid {
                columns: 7
                rows: 1
                spacing: 3
                Repeater{
                    model: 7

                    Rectangle{
                        width: 80;
                        height: 20
                        border.color: "black";
                        border.width: 1;
                        color: "#d8d8d8"
                        Text{
                            property variant items: ["Sunday","Monday","Tuesday","Wednesday","Thurday","Friday","Caturday"];
                            property variant items2: ["domingo","lunes","martes","miércoles","jueves","viernes","sábado"];
                            text: if(main.lang=="1")items[index]; else items2[index];
                            font.letterSpacing: 1
                            font.pointSize: 10
                            anchors.centerIn: parent
                        }
                    }
                }
            }
        }
        Rectangle{
            y: 70;
            width: 579;
            height: 530;
            color: "#bfbfbf";

            Grid {
                id:dayGrid
                columns: 7
                rows: 6
                spacing: 3
                Repeater{
                    id: repeatDays
                    model: (daysInMonth[CalFunctions.monthIndex]+CalFunctions.first_DOW);

                    Rectangle{

                        id: day;
                        border.color: "black"
                        border.width: 1;
                        width: 80;
                        height: 80
                        enabled:if(index<CalFunctions.first_DOW ){
                                    false;

                                }
                                else if(index<daysInMonth[CalFunctions.monthIndex]+CalFunctions.first_DOW){
                                        true;
                                }
                        color: if(index<CalFunctions.first_DOW ){
                                   "gray"

                               }
                               else if(index<daysInMonth[CalFunctions.monthIndex]+CalFunctions.first_DOW){
                                       if(CalFunctions.day===(index+1))
                                            "limegreen"
                                        else
                                           "green"
                               }


                        Text{
                            font.bold: true;
                            text: if(index<CalFunctions.first_DOW||index>=daysInMonth[CalFunctions.monthIndex]+CalFunctions.first_DOW){
                                      ""
                                  }
                                  else{
                                      index-CalFunctions.first_DOW+1;
                                      //console.log(index);
                                  }
                            font.pointSize: 7

                        }

                        MouseArea{
                            z:10
                            anchors.fill: parent
                            acceptedButtons: Qt.LeftButton;
                            property int dayClick:   index-CalFunctions.first_DOW+1
                            onClicked:{
                                onClicked: dayView.visible = true, dayView.setDate(CalFunctions.monthIndex, dayClick, CalFunctions.calcYear());

                            }
                        }
                    }
                }
            }
        }


        Item{
            width:300;
            height:500;
            anchors.centerIn: parent;
            DayView{
                id:dayView;
                visible:false;
                onVisibleChanged: dayView.lang = main.lang;
            }

        }
}


//Dan
//work in progress

/* Calculate leap year
if (this.month == 1) { // February only!
  if ((this.year % 4 == 0 && this.year % 100 != 0) || this.year % 400 == 0){
    monthLength = 29;
  }
}
  */
